package classpath

import (
	"os"
	"path/filepath"
	"fmt"

)

type Classpath struct {
	bootClasspath Entry
	extClasspath Entry
	userClasspath Entry
}

func Parse(jreOption string, cpOption string) *Classpath {
	cp := &Classpath{}
	cp.parseBootAndExtClasspath(jreOption)
	cp.parseUserClasspath(cpOption)
	return cp
}

func (self *Classpath) ReadClass(className string) ([] byte, Entry, error) {
	className = className + ".class"
	if data,entry,err := self.bootClasspath.readClass(className);err == nil {
		return data,entry,err
	}

	if data,entry,err := self.extClasspath.readClass(className);err == nil {
		return data,entry,err
	}

	return self.userClasspath.readClass(className)
}

func (self *Classpath) String() string {
	return self.userClasspath.String()
}

func (self *Classpath) parseBootAndExtClasspath(jreOption string) {
	jreDir := getJreDir(jreOption)
	jreLibPath := filepath.Join(jreDir, "jre/lib", "*")
	self.bootClasspath = newWildcardEntry(jreLibPath)
	jreExtPath := filepath.Join(jreDir, "lib", "*")
	self.extClasspath = newWildcardEntry(jreExtPath)
}

func getJreDir(jreOption string) string {
	if jreOption != "" && exists(jreOption) {
		return jreOption
	}

	if exists("./jre") {
		return "./jre"
	}

	if jh := os.Getenv("JAVA_HOME");jh != "" {
		fmt.Println("find JAVA_HOME, " + jh)
		return filepath.Join(jh, "jre")
	}

	panic("can't nt find jre folder")
}

func exists(path string) bool {
	if _,err := os.Stat(path);err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}

	return true
}

func (self *Classpath) parseUserClasspath(cpOption string) {
	if cpOption == "" {
		cpOption = "."
	}

	self.userClasspath = newEntry(cpOption)
}


